package com.situ.crm.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.fabric.xmlrpc.base.Array;
import com.situ.crm.common.ServerResponse;
import com.situ.crm.entity.Customer;
import com.situ.crm.entity.CustomerLoss;
import com.situ.crm.entity.Order;
import com.situ.crm.mapper.CustomerLossMapper;
import com.situ.crm.mapper.CustomerMapper;
import com.situ.crm.mapper.OrderMapper;
import com.situ.crm.service.ICustomerLossService;

@Service
public class CustomerLossServiceImpl implements ICustomerLossService {
	@Autowired
	private CustomerLossMapper customerLossMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private OrderMapper orderMapper;
	//分页
	@Override
	public ServerResponse pageList(Integer page, Integer limit, CustomerLoss customerLoss) {
		//分页工具
		PageHelper.startPage(page,limit);
		//查询数据
		List<CustomerLoss> list = customerLossMapper.pageList(customerLoss);
		//封装数据
		PageInfo pageInfo = new PageInfo<>(list);
		Integer count = (int) pageInfo.getTotal();
		
		return ServerResponse.createSuccess("查找成功", count, list);
	}
	
	//根据id删除
	@Override
	public ServerResponse deleteById(Integer id) {
		try {
			Integer count = customerLossMapper.deleteByPrimaryKey(id);
			if (count == 1) {
				return ServerResponse.createSuccess("删除成功");
			} else {
				return ServerResponse.createError("删除失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("删除失败");
		}
	}

	//批量删除
	@Override
	public ServerResponse deleteAll(String ids) {
		String[] idArrays = ids.split(",");
		try {
			Integer count = customerLossMapper.deleteAll(idArrays);
			if (count >= 1) {
				return ServerResponse.createSuccess("删除成功");
			} else {
				return ServerResponse.createError("删除失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("删除失败");
		}
	}

	//修改查询
	@Override
	public CustomerLoss select(Integer id) {
		return customerLossMapper.selectByPrimaryKey(id);
	}

	//修改
	@Override
	public ServerResponse update(CustomerLoss customerLoss) {
		try {
			Integer count = customerLossMapper.updateByPrimaryKeySelective(customerLoss);
			if (count == 1) {
				return ServerResponse.createSuccess("修改成功");
			} else {
				return ServerResponse.createError("修改失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("修改失败");
		}
	}
	
	//定时任务
	@Override
	public void checkCustomerLoss() {
		// 1.查找所有流失的客户(在订单表里面6个月都没有订单)
		List<Customer> customerList = customerMapper.findLossCustomer();
		for (Customer customer : customerList) {
			//填充customerLoss表
			CustomerLoss customerLoss = new CustomerLoss();
			customerLoss.setCustomerId(customer.getId());//客户编号
			customerLoss.setCustomerName(customer.getName());//客户姓名
			//查找客户最后一次订单
			Order order = orderMapper.findLastOrderByCustomerId(customer.getId());
			if (order != null) {
				customerLoss.setLastOrderTime(order.getOrderDate());//上次下单日期
			} else {
				customerLoss.setLastOrderTime(null);
			}
			
			Date date = new Date();
			customerLoss.setConfirmLossTime(date);//确定流失日期
			customerLoss.setStatus(0);//流失状态
			customerLoss.setLossReason("车换这麽勤干嘛！");
			//添加到客户流失表
			customerLossMapper.insert(customerLoss);
			
			// 4.客户表里面把客户状态修改为1：流失状态
			/*customer.setStatus(1);
			customerMapper.updateByPrimaryKeySelective(customer);*/
		}
		
	}

	//客户流失改状态
	@Override
	public ServerResponse upStatus(Integer id) {
		try {
			Integer count = customerLossMapper.upStatus(id);
			if (count == 1) {
				return ServerResponse.createSuccess("修改成功");
			} else {
				return ServerResponse.createError("修改失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("修改失败");
		}
	}
}
